package com.wxy.customview;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 *  在当前View中居中
 *     当前View的中点   getContentHeight/2
 *     当前文字中点   (fm.bottom - fm.top)/2
 *     获取当前文字中点到baseLine距离   (fm.bottom - fm.top)/2 -fm.bottom  --> fm.top/2 +fm.bottom/2
 *     *在当前View中baseLine的位置
 *          getContentHeight/2  - fm.top/2 -fm.bottom/2   -->  (getContentHeight - fm.top - fm.bottom)/2
 *
 *  算法 主要：
 *          记录三个区域 1.不变的部分 2.原有的部分 3.变化的部分
 *          记录三个文字的点位置   2.3部分x为 1部分的width
 *
 *          当数字增加时,原有部分上移,变化部分上移
 *               减少时,原有部分下移,变化部分下移
 */
public class CountView extends View implements View.OnClickListener {
    private static final int DEFAULT_TEXT_SIZE = 15;
    private int mTextSize = DEFAULT_TEXT_SIZE;
    private int mCount;
    private Paint mTextPaint;
    private boolean mIsThumbUp;
    private String[] mTexts;
    private TuvPoint[] mTextPoints;
    private int UpY;
    private float mTopUpY;
    private float mBottomUpY;
    private float mBaseLine;
    private Paint.FontMetrics fontMetrics;

    public CountView(Context context) {
        this(context, 0);
    }

    public CountView(Context context, int count) {
        this(context, null, count);
    }

    public CountView(Context context, @Nullable AttributeSet attrs, int count) {
        this(context, attrs, -1, count);
    }

    public CountView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int count) {
        super(context, attrs, defStyleAttr);
        mCount = count;
        init();
        setOnClickListener(this);
    }

    private void init() {
        mTexts = new String[3];
        mTextPoints = new TuvPoint[3];
        mTextPoints[0] = new TuvPoint();
        mTextPoints[1] = new TuvPoint();
        mTextPoints[2] = new TuvPoint();
        //初始值
        changeNum(0);


        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setColor(Color.parseColor("#000000"));
        mTextPaint.setTextSize(TuvUtils.sp2px(getContext(), mTextSize));

        fontMetrics = mTextPaint.getFontMetrics();
        mBaseLine = (getContentHeight() - fontMetrics.top - fontMetrics.bottom) / 2;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(TuvUtils.getDefaultSize(widthMeasureSpec, getPaddingLeft() + getContentWidth() + getPaddingRight()),
                TuvUtils.getDefaultSize(heightMeasureSpec, getPaddingTop() + getContentHeight() + getPaddingBottom()));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float v = mTextPaint.measureText(mTexts[0]);
        canvas.drawText(mTexts[0], 0, mBaseLine, mTextPaint);
        canvas.drawText(mTexts[1], v, mBaseLine - mTopUpY, mTextPaint);
        canvas.drawText(mTexts[2], v, mBaseLine + mBottomUpY, mTextPaint);
    }

    public int getContentWidth() {
        return (int) Math.ceil(mTextPaint.measureText(mCount + ""));
    }

    public int getContentHeight() {
        return TuvUtils.sp2px(getContext(), mTextSize);
    }

    @Override
    public void onClick(View view) {
        mIsThumbUp = !mIsThumbUp;
        if (mIsThumbUp) {
            changeNum(1);
        } else {
            changeNum(-1);
        }
    }

    public void change() {
        mIsThumbUp = !mIsThumbUp;
        if (mIsThumbUp) {
            changeNum(1);
        } else {
            changeNum(-1);
        }
    }

    private void startAnim() {
        ObjectAnimator animator = ObjectAnimator.ofFloat(this, "TopUp", mIsThumbUp ? 0 : 0, mIsThumbUp ? getContentHeight() : -getContentHeight());
        animator.setDuration(150);
        animator.start();

//        Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
        ObjectAnimator animator2 = ObjectAnimator.ofFloat(this, "BottomUp", mIsThumbUp ? getContentHeight() : -getContentHeight(), mIsThumbUp ? 0 : 0);
        animator2.setDuration(150);
        animator2.start();
    }

    public void setTopUp(float value) {
        mTopUpY = value;
        postInvalidate();
    }

    public void setBottomUp(float value) {
        mBottomUpY = value;
        postInvalidate();
    }

    public int getTextSize() {
        return mTextSize;
    }

    public void setCount(int count) {
        mCount = count;
    }

    private void changeNum(int num) {
        if (num == 0) {
            mTexts[0] = mCount + "";
            mTexts[1] = "";
            mTexts[2] = "";
            return;
        }
        String oldNum = String.valueOf(mCount);
        String newNum = String.valueOf(mCount + num);
        for (int i = 0; i < oldNum.length(); i++) {
            if (oldNum.charAt(i) != newNum.charAt(i)) {
                mTexts[0] = i == 0 ? "" : newNum.substring(0, i);
                mTexts[1] = oldNum.substring(i);
                mTexts[2] = newNum.substring(i);
                mCount += num;
                startAnim();
                requestLayout();
                break;
            }
        }
    }

}
